<h2>Active <a id="cart_pos">Cart</a>&nbsp;
<?php
$customer_id = $_SESSION["login.id"];
if(!empty($_SESSION["cart.id"]))
{
	$cart_id = $_SESSION["cart.id"];
	echo 
	"#$cart_id <input checked=\"yes\" id=\"cart_checkbox\" type=\"checkbox\"></h2>
	<div class=\"\" id=\"cart_table\">";
	if(!empty($_SESSION["cart.warning_msg"]))
	{
		echo "<div class=\"";
		if(!empty($_SESSION["cart.warning_box"]))
			echo $_SESSION["cart.warning_box"];
		else
			echo "warning";
		echo "_box\">".$_SESSION["cart.warning_msg"]."</div>";
		unset($_SESSION["cart.warning_msg"]);
		unset($_SESSION["cart.warning_box"]);
	}
	$check_cart1 = mysql_num_rows(mysql_query("SELECT * FROM web_cart WHERE customer_id = $customer_id AND status = 1"));
	$check_cart2 = mysql_num_rows(mysql_query("SELECT * FROM web_cart WHERE id = $cart_id AND customer_id = $customer_id AND status = 1"));
	if($check_cart1 > 1 || $check_cart2 < 1)
	{
		mysql_query("UPDATE web_cart SET status = 2 WHERE customer_id = $customer_id AND status <> 0");
		unset($_SESSION["cart.id"]);
		$_SESSION["cart.warning_box"] = "error";
		$_SESSION["cart.warning_msg"] = "Terjadi konflik pada sistem cart, silahkan mengulang proses pemesanan barang";
		die("<meta content='0;URL=?m=barang' http-equiv='refresh' />");
	}	
?>
	<table class="rounded-corner">
		<thead>
			<tr>
				<th scope="col" class="rounded-company"></th>
				<th colspan="2" scope="col" class="rounded">Nama</th>
				<th scope="col" class="rounded">Jumlah</th>
				<th scope="col" class="rounded">Satuan</th>
				<th scope="col" class="rounded">Jumlah unit</th>
				<th scope="col" class="rounded">Satuan unit</th>
				<th scope="col" class="rounded">Harga</th>
				<th scope="col" class="rounded">Diskon</th>
				<th scope="col" class="rounded">Netto</th>
				<th scope="col" class="rounded">Total</th>
				<th scope="col" class="rounded-q4">Action</th>
			</tr>
		</thead>
		<tbody>
<?php
	$posting = true;
	$posting_msg = "";
	if($check_customer > 0)
	{
		$posting = false;
		$posting_msg .= " Anda harus menentukan password terlebih dahulu untuk dapat memesan barang. ";
	}
	$get_detail = mysql_query("
	SELECT d.id, d.good_id AS NomorBarang, b.vcNamaBeli AS NamaBarang, b.intNomorMBrand AS NomorBrand, 
	d.unit1_id AS NomorSatuanHarga, sh.vcNama AS NamaSatuanHarga, d.unit2_id AS NomorSatuanUnit, su.vcNama AS NamaSatuanUnit
	FROM web_cart_detail d 
	JOIN mbarang b ON d.good_id = b.intNomor
	JOIN msatuan sh ON d.unit1_id = sh.intNomor
	JOIN msatuan su ON d.unit2_id = su.intNomor
	WHERE d.cart_id = $cart_id AND d.status = 1 AND b.intStatus = 1 AND b.intAktif = 1");
	$no = 1;
	$total_akhir = 0;
	while($r = mysql_fetch_array($get_detail))
	{
		$check_detail = mysql_num_rows(mysql_query("
		SELECT d.id, d.good_id AS NomorBarang, b.vcNamaBeli AS NamaBarang, 
		d.unit1_id AS NomorSatuanHarga, sh.vcNama AS NamaSatuanHarga, d.unit2_id AS NomorSatuanUnit, su.vcNama AS NamaSatuanUnit
		FROM web_cart_detail d 
		JOIN mbarang b ON d.good_id = b.intNomor
		JOIN msatuan sh ON d.unit1_id = sh.intNomor
		JOIN msatuan su ON d.unit2_id = su.intNomor
		WHERE d.cart_id = $cart_id AND d.status = 1 AND b.intStatus = 1 AND b.intAktif = 1 AND d.good_id = ".$r["NomorBarang"]));
		if($check_detail > 1)
		{
			$posting = false;
			$posting_msg .= " Terdapat data barang yang kembar atas nama ".$r["NamaBarang"].". ";
		}
		$jumlah_unit = 0;
		$html_shade = "";
		$get_shade = mysql_query("SELECT * FROM web_cart_shade WHERE cart_detail_id = ".$r["id"]." AND status = 1");
		$count_shade = mysql_num_rows($get_shade);
		if($count_shade > 0)
		{
			while($shade = mysql_fetch_array($get_shade))
			{
				$jumlah_unit += $shade["quantity"];
				$html_shade .= "
				<tr>
				<td></td>
				<td>".$shade["shade"]."</td>
				<td>".number_format($shade["quantity"],$_SESSION["setting.decimal"])."</td>
				<td>
					<a href=\"?m=order&md=cart_shade_edit&id=".$shade["id"]."\"><img src=\"../images/user_edit.png\" border=\"0\" /></a>
					<a class=\"ask\" href=\"?m=order&md=cart_shade_delete&id=".$shade["id"]."\"><img src=\"../images/trash.png\" border=\"0\" /></a>
				</td>
				<td colspan=\"8\"></td>
				</tr>";
				$check_shade = mysql_num_rows(mysql_query("SELECT * FROM web_cart_shade WHERE cart_detail_id = ".$r["id"]." AND status = 1 AND shade = '".$shade["shade"]."'"));
				if($check_shade > 1)
				{
					$posting = false;
					$posting_msg .= " Terdapat redudansi data shade ".$shade["shade"]." pada barang ".$r["NamaBarang"].". ";
				}
			}		
		}
		else
		{
			$posting = false;
			$posting_msg .= " Shade dari barang ".$r["NamaBarang"]." belum ditentukan. ";
		}
		if($jumlah_unit <= 0)
		{
			$posting = false;
			$posting_msg .= " Jumlah shade dari barang ".$r["NamaBarang"]." tidak valid. ";
		}
		$valuta = $_SESSION["setting.valuta"];
		$nomor_barang = $r["NomorBarang"];
		$get_satuan_harga = mysql_fetch_array(mysql_query("
		SELECT intNomor AS NomorSatuan, vcNama AS NamaSatuan, intNomorMSatuan1 AS NomorSatuan1, decKonversi AS NilaiKonversi, decHarga AS PriceList
		FROM (
			SELECT 1 AS intOrder, b.intNomor , b.vcNama, a.intNomorMSatuan1 AS intNomorMSatuan1, 1 AS decKonversi,
				CASE
					WHEN $valuta = 1 THEN a.decHargaPL1
					WHEN $valuta = 2 THEN a.decHargaPL1USD
					WHEN $valuta = 3 THEN a.decHargaPL1RMB
				END AS decHarga
			FROM MBarang a JOIN MSatuan b ON b.intNomor = a.intNomorMSatuan1 WHERE a.intNomor = $nomor_barang AND b.intNomor > 0
			UNION ALL
			SELECT 2 AS intOrder, b.intNomor , b.vcNama, a.intNomorMSatuan1 AS intNomorMSatuan1, decKonversi2 AS decKonversi,
				CASE
					WHEN $valuta = 1 THEN a.decHargaPL2
					WHEN $valuta = 2 THEN a.decHargaPL2USD
					WHEN $valuta = 3 THEN a.decHargaPL2RMB
				END AS decHarga
			FROM MBarang a JOIN MSatuan b ON b.intNomor = a.intNomorMSatuan2 WHERE a.intNomor = $nomor_barang AND b.intNomor > 0
			UNION ALL
			SELECT 3 AS intOrder, b.intNomor , b.vcNama, a.intNomorMSatuan1 AS intNomorMSatuan1, decKonversi2 * decKonversi3 AS decKonversi,
				CASE 
					WHEN $valuta = 1 THEN a.decHargaPL3
					WHEN $valuta = 2 THEN a.decHargaPL3USD
					WHEN $valuta = 3 THEN a.decHargaPL3RMB
				END AS decHarga
			FROM MBarang a JOIN MSatuan b ON b.intNomor = a.intNomorMSatuan3
			WHERE a.intNomor = $nomor_barang AND b.intNomor > 0)
		a WHERE a.intNomor = ".$r["NomorSatuanHarga"]." ORDER BY a.intOrder"));
		$harga = $get_satuan_harga["PriceList"];
		if(empty($harga))
		{
			$harga = 0;
			$posting = false;
			$posting_msg .= " Satuan dari barang ".$r["NamaBarang"]." tidak valid. ";
		}
		$get_satuan_unit = mysql_fetch_array(mysql_query("
		SELECT intNomor AS NomorSatuan, vcNama AS NamaSatuan, decKonversi AS Konversi,
			CASE
				WHEN intNomor = 0 THEN 0
				WHEN intNomor = intNomorMSatuan1 THEN 1
				WHEN intNomor = intNomorMSatuan2 THEN decKonversi2
				WHEN intNomor = intNomorMSatuan3 THEN decKonversi2 * decKonversi3
			ELSE 0
			END AS KonversiSatuan
		FROM (
			SELECT 1 AS intOrder, b.intNomor , b.vcNama,
				CASE
					WHEN 0 = 0 THEN 0
					WHEN 0 = intNomorMSatuan1 THEN 1
					WHEN 0 = intNomorMSatuan2 THEN 1/decKonversi2
					WHEN 0 = intNomorMSatuan3 THEN 1/(decKonversi2 * decKonversi3)
				ELSE 0
				END AS decKonversi,
			a.intNomorMSatuan1, a.intNomorMSatuan2, a.intNomorMSatuan3, a.decKonversi2, a.decKonversi3
			FROM MBarang a JOIN MSatuan b ON b.intNomor = a.intNomorMSatuan1
			WHERE a.intNomor = $nomor_barang AND b.intNomor > 0
			UNION ALL
			SELECT 2 AS intOrder, b.intNomor , b.vcNama,
				CASE 
					WHEN 0 = 0 THEN 0
					WHEN 0 = intNomorMSatuan1 THEN decKonversi2
					WHEN 0 = intNomorMSatuan2 THEN 1
					WHEN 0 = intNomorMSatuan3 THEN 1/decKonversi3
				ELSE 0
				END AS decKonversi,
			a.intNomorMSatuan1, a.intNomorMSatuan2, a.intNomorMSatuan3, a.decKonversi2, a.decKonversi3
			FROM MBarang a JOIN MSatuan b ON b.intNomor = a.intNomorMSatuan2
			WHERE a.intNomor = $nomor_barang AND b.intNomor > 0 AND a.decKonversi2 > 0
			UNION ALL
			SELECT 3 AS intOrder, b.intNomor , b.vcNama,
				CASE
					WHEN 0 = 0 THEN 0
					WHEN 0 = intNomorMSatuan1 THEN (decKonversi2 * decKonversi3)
					WHEN 0 = intNomorMSatuan2 THEN decKonversi3
					WHEN 0 = intNomorMSatuan3 THEN 1
				ELSE 0
				END AS decKonversi,
			a.intNomorMSatuan1, a.intNomorMSatuan2, a.intNomorMSatuan3, a.decKonversi2, a.decKonversi3
			FROM MBarang a JOIN MSatuan b ON b.intNomor = a.intNomorMSatuan3
			WHERE a.intNomor = $nomor_barang AND b.intNomor > 0 AND a.decKonversi3 > 0)
		a WHERE a.intNomor = ".$r["NomorSatuanUnit"]." ORDER BY a.intOrder"));
		$konversi_unit = $get_satuan_unit["KonversiSatuan"];
		if(empty($konversi_unit))
		{
			$konversi_unit = 0;
			$posting = false;
			$posting_msg .= " Satuan unit dari barang ".$r["NamaBarang"]." tidak valid. ";
		}
		$get_diskon = mysql_fetch_array(mysql_query("SELECT discount_rate AS DiskonBrand FROM web_discount WHERE customer_id = ".$_SESSION["login.id"]." AND brand_id = ".$r["NomorBrand"]." AND status = 1"));
		$diskon = $get_diskon["DiskonBrand"];
		if(empty($diskon))
			$diskon = 0;
		$jumlah_harga = $jumlah_unit * $konversi_unit;
		$netto = $harga - ($harga * $diskon / 100);
		$total = $jumlah_harga * $netto;
		$total_akhir += $total;
		echo"
		<tr>
		<td>$no</td>
		<td colspan=\"2\">".$r["NamaBarang"]."</td>
		<td>".number_format($jumlah_harga,$_SESSION["setting.decimal"],",",".")."</td>
		<td>".$r["NamaSatuanHarga"]."</td>
		<td>".number_format($jumlah_unit,$_SESSION["setting.decimal"],",",".")."</td>
		<td>".$r["NamaSatuanUnit"]."</td>
		<td>".number_format($harga,$_SESSION["setting.money"],",",".")."</td>
		<td>$diskon%</td>
		<td>".number_format($netto,$_SESSION["setting.money"],",",".")."</td>
		<td>".number_format($total,$_SESSION["setting.money"],",",".")."</td>
		<td>
			<a href=\"?m=order&md=cart_edit&id=".$r["id"]."\"><img src=\"../images/user_edit.png\" border=\"0\" /></a>
			<a class=\"ask\" href=\"?m=order&md=cart_delete&id=".$r["id"]."\"><img src=\"../images/trash.png\" border=\"0\" /></a>
		</td>
		</tr>
		$html_shade";
		$no++;
	}
?>		</tbody>
		<tfoot>
			<tr>
				<td align="right" colspan="10" class="rounded-foot-left"><b>TOTAL</b> : </td>
				<td align="right"><?php echo number_format($total_akhir,$_SESSION["setting.money"],",","."); ?></td>
				<td class="rounded-foot-right">&nbsp;</td>
			</tr>
		</tfoot>
	</table>
	<a href="#cart_pos" class="bt_blue" onclick="<?php
	if($posting)
		echo "link_confirmation('Apakah Anda yakin akan mengorder barang-barang di dalam cart ini?','preview.php?mdl=checkout')";
	else
		echo "alert('$posting_msg')";
	?>">
		<span class="bt_blue_lft"></span>
		<strong>Checkout</strong>
		<span class="bt_blue_r"></span>
	</a>
	<a href="#cart_pos" class="bt_red" onclick="link_confirmation('Apakah Anda yakin ingin membatalkan cart ini?','?m=order&md=cart_discard&id=<?php echo $cart_id; ?>')">
		<span class="bt_red_lft"></span>
		<strong>Discard</strong>
		<span class="bt_red_r"></span>
	</a>
</div>
<?php
}
else
{
	mysql_query("UPDATE web_cart SET status = 2 WHERE customer_id = $customer_id AND status <> 0");
	echo 
	"<input id=\"cart_checkbox\" type=\"checkbox\"></h2>
	<div class=\"hiding\" id=\"cart_table\">No active cart.</div>";
}
?>